package cn.wenhaha.app.data.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.wenhaha.app.data.bean.vo.DataPageURL;
import cn.wenhaha.app.data.service.DataUserService;
import cn.wenhaha.common.core.domain.AjaxV1Result;
import cn.wenhaha.datasource.DataUser;
import cn.wenhaha.datasource.Obj;
import cn.wenhaha.datasource.ObjInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 用户类
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-07-14 21:47
 */
@RestController
@RequestMapping("/api/v1/data/user")
@Slf4j
public class ApiDataUserController {

    @Autowired
    private DataUserService dataUserService;

    @GetMapping("add")
    @SaCheckLogin
    public AjaxV1Result<List<DataPageURL>> addPageList(){
        return AjaxV1Result.newSuccess(dataUserService.addPageList());
    }

    @GetMapping("remove/{code}/{id}")
    @SaCheckLogin
    public AjaxV1Result<Boolean> remove(@PathVariable("id") String id,
                                  @PathVariable("code") String code){
        return AjaxV1Result.newSuccess(dataUserService.remove(code,id));
    }



    @GetMapping("list")
    @SaCheckLogin
    public AjaxV1Result<List<DataUser>> list(){
        return AjaxV1Result.newSuccess(dataUserService.getUserList());
    }


    @GetMapping("/update/{code}/{id}")
    @SaCheckLogin
    AjaxV1Result<?> updatePage(@PathVariable("id") String id,
                   @PathVariable("code") String code) {
        String updateUrl = dataUserService.getUpdateUrl(code, id);
        Map<String, String> build = MapUtil.builder("url", updateUrl)
                .put("key", StpUtil.getTokenValue()).build();
        return  AjaxV1Result.newSuccess(build);
    }



    @GetMapping("/objects/{code}/{id}")
    @SaCheckLogin
    AjaxV1Result<List<Obj>> objectsList(@PathVariable("id") String id,
                   @PathVariable("code") String code) {
        List<Obj> objs = dataUserService.objectList(code, id);
        return  AjaxV1Result.newSuccess(objs);
    }



    @GetMapping("/objects/info")
    @SaCheckLogin
    AjaxV1Result<ObjInfo> objectInfo(@RequestParam("apiName") String apiName,
                                       @RequestParam("id") String id, @RequestParam("code") String code) {
        ObjInfo objs = dataUserService.objectInfo(code, id,apiName);
        return  AjaxV1Result.newSuccess(objs);
    }



}
